<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.4.1.min.js"></script>
    <script>
        $(function () {
            var stuno=window.sessionStorage.getItem("stuno");
            var gradeid=0;
            $.get("student/get/"+stuno,function (result) {
                 //alert(result);
                 //result=JSON.parse(result);
                 if(result.success==false){
                     alert("出现错误:"+result.msg);
                     history.back();
                     return;
                 }
                 let student=result.data;
                gradeid=student.gradeid;
                 //...$("#sname").val(student.sname);//行，工作量大
                for(var p in student){
                    $("input[name="+p+"]:not(:radio,:checkbox,:file)").val(student[p]);
                }
                //单独处理，sex
                $(":radio[name=sex][value="+student.sex+"]").prop("checked",true);
                //照片
                $("img#idcardpic").attr("src","images/"+student.idcardpic);
               //
            });


            $.get("grade/findALL",function (result) {
                var $g=$("[name=gradeid]");
                $.each(result.data,function (i,d) {
                    $g.append(new Option(d.gname,d.id));
                });
            }).done(function () {
                $("[name=gradeid]").val(gradeid);
            });

            $("#submit").click(function () {
                var formdata=new FormData($("form")[0]);
                $.ajax({
                    url:'student/update',
                    type:'post',
                    data:formdata,
                    processData:false,
                    contentType:false,
                    cache:false,
                    success:function (result) {
                        //result=JSON.parse(result);
                        if(result.success)
                        {
                            alert("修改成功");
                            location.href='students.html';
                            return;
                        }
                        alert("修改失败："+result.msg);
                    },
                    error:function (result) {
                        alert(result.msg);
                    }
                });
                return false;
            });
        });
    </script>
</head>
<body>
<!-- -->
<form action="student/update" method="post" >
     <input type="hidden" name="stuno" />
    <fieldset>
        <legend>学生信息</legend>
        <label for="">
            姓名: <input type="text" pattern="[\u4E00-\u9FA5]{2,10}" required autofocus placeholder="请输入姓名"  name="sname">
        </label>
        <label for="">
            性别:
            <div id="gender">
                <input type="radio" name="sex" value="男" checked="checked" class="gender"  /> &nbsp;男&nbsp;&nbsp;&nbsp;
                <input type="radio" name="sex" value="女" class="gender"/>  &nbsp;女&nbsp;
                <input type="radio" name="sex" value="妖" class="gender"/> &nbsp;妖&nbsp;
            </div>
        </label>
        <label for="">手机号码: <input type="tel" pattern="1[3578]\d{9}" placeholder="请输入手机号码" name="telephone" id="telephone">
        </label>
        <label for="">QQ: <input type="text" placeholder="请输入QQ" name="qq">
        </label>
        <label for="">微信: <input type="text" placeholder="请输入微信" name="wechat" >
        </label>

        <label for="">
            年级:
            <select name="gradeid" id="gradeid">

            </select>
        </label>
        <label for="">
            入学日期: <input name="enterdate" type="date" >
        </label>
        <label for="">起运地: <input type="text" placeholder="请输入起运地" name="fromcity" >
        </label>
        <label for="">
            生日: <input name="birthday" type="date" >
        </label>
        <label for="">住址: <input type="text" placeholder="请输入住址" name="address" >
        </label>
      <!--  <%&#45;&#45; <label for="">密码: <input type="password" placeholder="请输入密码" name="pass">
    </label>&#45;&#45;%>-->
        <label for="">居民身份证图片: <input type="file" placeholder="请输入居民身份证图片" name="idcard">
        </label>
        <img src="#" alt="没有照片" id="idcardpic" height="100px" width="150px"/>
        <label for="" class="btn">
            <input type="button" value="保存" style="font-size: 22px" id="submit">
        </label>
    </fieldset>
</form>
</body>
</html>